package com.dt.controller;

import com.dt.common.exception.ExceptionPrintUtil;
import com.dt.common.response.ResponseCodeEnum;
import com.dt.common.response.ResponseData;
import com.dt.entity.Employee;
import com.dt.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 员工相关接口
 *
 * @author JOSWAY
 */
@Api(tags = "员工相关接口")
@CrossOrigin
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    private final static Logger LOGGER = LoggerFactory.getLogger(EmployeeController.class);
    @Resource
    EmployeeService employeeService;

    @ApiIgnore
    @ApiOperation(value = "删除员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "employeeId", value = "员工主键", required = true, dataType = "int", paramType = "query", example = "10086")
    })
    @DeleteMapping(value = "delete-employee")
    public ResponseData<Integer> deleteEmployee(Integer employeeId) {
        try {
            return new ResponseData<>(ResponseCodeEnum.SUCCESS, "成功", employeeService.deleteByPrimaryKey(employeeId));
        } catch (Exception e) {
            LOGGER.error(ExceptionPrintUtil.stackLogRecord(e));
            return new ResponseData<>(ResponseCodeEnum.FAILED, ExceptionPrintUtil.stackLogRecord(e));
        }
    }

    @ApiOperation(value = "添加员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "employeeId", value = "员工主键", required = true, dataType = "int", paramType = "query", example = "10086"),
            @ApiImplicitParam(name = "employeeName", value = "员工名称", required = true, dataType = "string", paramType = "query", example = "小明"),
            @ApiImplicitParam(name = "departmentId", value = "部门编号", required = true, dataType = "int", paramType = "query", example = "10010")
    })
    @PostMapping(value = "insert-employee")
    public ResponseData<Integer> insertEmployee(Integer employeeId, String employeeName, Integer departmentId) {
        try {
            Employee employee = new Employee();
            employee.setEmployeeId(employeeId);
            employee.setEmployeeName(employeeName);
            employee.setDepartmentId(departmentId);

            return new ResponseData<>(ResponseCodeEnum.SUCCESS, "成功", employeeService.insertSelective(employee));
        } catch (Exception e) {
            LOGGER.error(ExceptionPrintUtil.stackLogRecord(e));
            return new ResponseData<>(ResponseCodeEnum.FAILED, ExceptionPrintUtil.stackLogRecord(e));
        }
    }

    @ApiOperation(value = "查询所有员工")
    @GetMapping(value = "select-all-employee")
    @Deprecated
    public ResponseData<List<Employee>> selectAllEmployee() {
        try {
            return new ResponseData<>(ResponseCodeEnum.SUCCESS, "成功", employeeService.selectAll());
        } catch (Exception e) {
            LOGGER.error(ExceptionPrintUtil.stackLogRecord(e));
            return new ResponseData<>(ResponseCodeEnum.FAILED, ExceptionPrintUtil.stackLogRecord(e));
        }
    }

    @ApiOperation(value = "查询员工信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "employeeId", value = "员工主键", required = true, dataType = "int", paramType = "query", example = "10086")
    })
    @GetMapping(value = "select-one-employee")
    public ResponseData<Employee> selectOneEmployee(Integer employeeId) {
        try {
            return new ResponseData<Employee>(ResponseCodeEnum.SUCCESS, "成功", employeeService.selectByPrimaryKey(employeeId));
        } catch (Exception e) {
            LOGGER.error(ExceptionPrintUtil.stackLogRecord(e));
            return new ResponseData<>(ResponseCodeEnum.FAILED, ExceptionPrintUtil.stackLogRecord(e));
        }
    }

    @ApiOperation(value = "修改员工信息")
    @PutMapping(value = "update-employee")
    public ResponseData<Integer> updateEmployee(Employee employee) {

        try {
            return new ResponseData<Integer>(ResponseCodeEnum.SUCCESS, "成功", employeeService.updateByPrimaryKeySelective(employee));
        } catch (Exception e) {
            LOGGER.error(ExceptionPrintUtil.stackLogRecord(e));
            return new ResponseData<>(ResponseCodeEnum.FAILED, ExceptionPrintUtil.stackLogRecord(e));
        }
    }
}
